//
//  HamatonMeViewController.swift
//  hamaton-app
//
//  Created by intyt on 2024/3/26.
//  我的

import UIKit
import SnapKit

class HamatonMeViewController: HamatonBaseViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Theme.Color.Background.default
        navigationItem.title = "我的"
        let margTop: Float = 15
        let margRight: Float = -20
        let margLeft: Float = 20
        // 联系我们
        view.addSubview(lianxiwomenLabel)
        view.addSubview(lianxiwomenRightImg)
        view.addSubview(lianxiwomenRightButton)
        view.addSubview(lineView1)
        // 关于
        view.addSubview(guanyuLabel)
        view.addSubview(guanyuRightImg)
        view.addSubview(guanyuRightButton)
        view.addSubview(lineView2)
        // 退出登录
        view.addSubview(loginOutBtn)
        // 联系我们
        lianxiwomenLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        
        lineView1.snp.makeConstraints { make in
            make.top.equalTo(lianxiwomenLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        
        lianxiwomenRightImg.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop+4)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        
        lianxiwomenRightButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(margTop)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView1.snp.top).offset(0)
        }
        // 关于
        guanyuLabel.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop)
            make.left.equalToSuperview().offset(margLeft)
            make.width.equalTo(100)
            make.height.equalTo(20)
        }
        guanyuRightImg.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(margTop+4)
            make.right.equalToSuperview().offset(margRight)
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        lineView2.snp.makeConstraints { make in
            make.top.equalTo(guanyuLabel.snp.bottom).offset(margTop)
            make.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
        guanyuRightButton.snp.makeConstraints { make in
            make.top.equalTo(lineView1.snp.bottom).offset(0)
            make.left.right.equalToSuperview()
            make.bottom.equalTo(lineView2.snp.top).offset(0)
        }
        
        loginOutBtn.snp.makeConstraints { make in
            make.top.equalTo(lineView2.snp.bottom).offset(margTop*2)
            make.right.equalToSuperview().offset(-20)
            make.left.equalToSuperview().offset(20)
            make.height.equalTo(40)
        }
         
    }
    // 联系我们
    private lazy var lianxiwomenLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "联系我们"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    lazy var lianxiwomenRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        return view
    }()
    lazy var lianxiwomenRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onSelectLianxiwomen), for: .touchUpInside)
        return button
    }()
    private lazy var lineView1: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    
    // 关于
    private lazy var guanyuLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = "关于"
        label.backgroundColor = .clear
        label.textAlignment = .left
        return label
    }()
    lazy var guanyuRightImg: UIImageView = {
        let view = UIImageView(image: UIImage(named: "Mine_More"))
        return view
    }()
    lazy var guanyuRightButton: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickGuanyu), for: .touchUpInside)
        return button
    }()
    
    private lazy var lineView2: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        return view
    }()
    // 退出登录
    lazy var loginOutBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setTitle(NSLocalizedString("退出登录", tableName: "HamatonLocalizable", comment: ""), for: .normal)
        button.setTitleColor(UIColor(hex6: 0xFFFFFF, alpha: 1.0), for: .normal)
        button.titleLabel!.font = Theme.Font.size(18, bold: true)
        button.addTarget(self, action: #selector(onClickButton), for: .touchUpInside)
        button.backgroundColor  = Theme.Color.Background.main
        button.layer.masksToBounds = true
        button.layer.cornerRadius = 10
        return button
    }()
    // 联系我们
    @objc func onSelectLianxiwomen() {
        let message = "电话：86-571-86222610\n邮箱：marketing@hamaton.com.cn\n"
        let alertController = UIAlertController(title: "联系我们\n", message: "", preferredStyle: .alert)
        weak var weakSelf = self
        let cancelAction = UIAlertAction(title: NSLocalizedString("取消", tableName: "HamatonLocalizable", comment: ""), style: .cancel, handler: nil)
        let confirmAction = UIAlertAction(title: NSLocalizedString("呼叫", tableName: "HamatonLocalizable", comment: ""), style: .default) { action in
            // weakSelf?.delegate?.upgradeSuccessDisconnect()
            let phoneNumber = "86222610" // 设置要拨打的电话号码
            if let phoneURL = URL(string: "tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(phoneURL) {
                UIApplication.shared.open(phoneURL, options: [:], completionHandler: nil)
            } else {
                // 如果设备不支持拨打电话功能或者电话号码无效，则显示警告信息
                self.view.makeToast("设备不支持拨打电话或号码无效", duration: 3.0, position: .center)
            }
        }
        // 创建一个 NSMutableParagraphStyle 对象
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 10 // 设置行间距
        // 创建属性字典
        let attributes: [NSAttributedString.Key: Any] = [
            .font: UIFont.systemFont(ofSize: 14), // 设置字体大小
            .foregroundColor: UIColor.black, // 设置字体颜色
            .paragraphStyle: paragraphStyle // 设置段落样式
        ]
        // 创建带有自定义属性的 NSAttributedString
        let attributedMessage = NSAttributedString(string: message, attributes: attributes)
        
        // 设置消息文本
        alertController.setValue(attributedMessage, forKey: "attributedMessage")
        alertController.addAction(cancelAction)
        alertController.addAction(confirmAction)
        
        self.present(alertController, animated: true, completion: nil)
    }
    // 关于
    @objc func onClickGuanyu() {
        let vc = HamatonAboutViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: false)
    }
    // 退出登录
    @objc func onClickButton() {
        print("退出登录逻辑")
        HamatonLoginManager.shared.removeToken()
        if UIApplication.shared.delegate != nil {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.resetRootViewController()
        }
    }
     

}
